﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlClient;

namespace DRSM
{
    class AffectedCityControl
    {
        //int count = 0;
        static int count = 0;
        //public static void addcity( TextBox tbxNameOfCity, TextBox tbxNoOfPopulation, TextBox tbxShortDescOfDamage, RadioButton radioBtnLow, RadioButton radioBtnMedium, RadioButton radioBtnHigh, CheckBox chkBxMedical, CheckBox chkBxMilitary, CheckBox chkBxVolunteer)
        public static void addcity( TextBox tbxNameOfCity, TextBox tbxNoOfPopulation, TextBox tbxShortDescOfDamage, RadioButton radioBtnLow, RadioButton radioBtnMedium, RadioButton radioBtnHigh, CheckBox chkBxMedical, CheckBox chkBxMilitary, CheckBox chkBxVolunteer)
        {
            count++;
            string nameOfCity = tbxNameOfCity.Text.Trim();
            string NoOfPopulation = tbxNoOfPopulation.Text.Trim();
            string ShortDescOfDamage = tbxShortDescOfDamage.Text.Trim();

            char lvlOfDisaster = ' ';
            if (radioBtnLow.Checked.Equals(true))
            {
                lvlOfDisaster = 'L';
            }
            else if (radioBtnMedium.Checked.Equals(true))
            {
                lvlOfDisaster = 'M';
            }
            else if (radioBtnHigh.Checked.Equals(true))
            {
                lvlOfDisaster = 'H';
            }

            //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
            string supportPersonnelRequired = " ";
            if (chkBxMedical.Checked.Equals(true))
            {
                supportPersonnelRequired = "MEDICAL";
            }
            if (chkBxMilitary.Checked.Equals(true))
            {
                supportPersonnelRequired = "MILITARY";
            }
            if (chkBxVolunteer.Checked.Equals(true))
            {
                supportPersonnelRequired = "VOLUNTEER";
            }
            if (chkBxMedical.Checked.Equals(true) && chkBxMilitary.Checked.Equals(true))
            {
                supportPersonnelRequired = "MEDICAL AND MILITARY";
            }
            if (chkBxMedical.Checked.Equals(true) && chkBxVolunteer.Checked.Equals(true))
            {
                supportPersonnelRequired = "MEDICAL AND VOLUNTEER";
            }
            if (chkBxMilitary.Checked.Equals(true) && chkBxVolunteer.Checked.Equals(true))
            {
                supportPersonnelRequired = "MILITARY AND VOLUNTEER";
            }
            if (chkBxMedical.Checked.Equals(true) && chkBxMilitary.Checked.Equals(true) && chkBxVolunteer.Checked.Equals(true))
            {
                supportPersonnelRequired = "MEDICAL AND MILITARY AND VOLUNTEER";
            }

            //generate random number
            int num = 10000;

            num = num + count;

            //if (AllFormClass.AffectedCity.Count > 0)
            //{
            //    num = 10000 + AllFormClass.AffectedCity.Count;
            //}
            //else
            //{
            //    num = 10000;
            //}

            //int num = (int)DateTime.Now.Ticks;

            dbmanager.addAffectedCity(nameOfCity, NoOfPopulation, lvlOfDisaster, supportPersonnelRequired, ShortDescOfDamage);
            //AffectedCity ac = new AffectedCity(num, tbxNameOfCity.Text, tbxNoOfPopulation.Text, lvlOfDisaster, supportPersonnelRequired, tbxShortDescOfDamage.Text);
           // AllFormClass.AffectedCity.Add(ac);
        }


        public static void updateAffectedCity(TextBox tbxUpdateCityId, TextBox tbxUpdateNameOfCity, TextBox tbxUpdateNoOfPopulation, TextBox tbxUpdateDescOfDmg, RadioButton radioBtnUpdateLow, RadioButton radioBtnUpdateMedium, RadioButton radioBtnUpdateHigh, CheckBox chkbxUpdateMedical, CheckBox chkbxUpdateMilitary, CheckBox chkbxUpdateVolunteer)
        {
            foreach (AffectedCity AffectedCity in AllFormClass.AffectedCity)
            {
                if (Convert.ToString(AffectedCity.CityID1) == tbxUpdateCityId.Text)
                {
                    AffectedCity.CityID1 = Convert.ToInt32(tbxUpdateCityId.Text);
                    AffectedCity.NameOfCity = tbxUpdateNameOfCity.Text;
                    AffectedCity.NoOfPopulation = tbxUpdateNoOfPopulation.Text;
                    AffectedCity.ShortDescOfDmg = tbxUpdateDescOfDmg.Text;

                    if (radioBtnUpdateLow.Checked.Equals(true))
                    {
                        AffectedCity.LvlOfDisaster = 'L';
                    }
                    else if (radioBtnUpdateMedium.Checked.Equals(true))
                    {
                        AffectedCity.LvlOfDisaster = 'M';
                    }
                    else if (radioBtnUpdateHigh.Checked.Equals(true))
                    {
                        AffectedCity.LvlOfDisaster = 'H';
                    }

                    if (chkbxUpdateMedical.Checked.Equals(true))
                    {
                        AffectedCity.SupportPersonnelNeeded = "MEDICAL";
                    }
                    if (chkbxUpdateMilitary.Checked.Equals(true))
                    {
                        AffectedCity.SupportPersonnelNeeded = "MILITARY";
                    }
                    if (chkbxUpdateVolunteer.Checked.Equals(true))
                    {
                        AffectedCity.SupportPersonnelNeeded = "VOLUNTEER";
                    }
                    if (chkbxUpdateMedical.Checked.Equals(true) && chkbxUpdateMilitary.Equals(true))
                    {
                        AffectedCity.SupportPersonnelNeeded = "MEDICAL AND MILITARY";
                    }
                    if (chkbxUpdateMedical.Checked.Equals(true) && chkbxUpdateVolunteer.Equals(true))
                    {
                        AffectedCity.SupportPersonnelNeeded = "MEDICAL AND VOLUNTEER";
                    }
                    if (chkbxUpdateMilitary.Checked.Equals(true) && chkbxUpdateVolunteer.Equals(true))
                    {
                        AffectedCity.SupportPersonnelNeeded = "MILITARY AND VOLUNTEER";
                    }
                    if (chkbxUpdateMedical.Checked.Equals(true) && chkbxUpdateMilitary.Checked.Equals(true) && chkbxUpdateVolunteer.Checked.Equals(true))
                    {
                        AffectedCity.SupportPersonnelNeeded = "MEDICAL AND MILITARY AND VOLUNTEER";
                    }
                   // dbmanager.updateAffectedCity(AffectedCity.CityID1, AffectedCity.NameOfCity, AffectedCity.NoOfPopulation, AffectedCity.LvlOfDisaster, AffectedCity.SupportPersonnelNeeded, AffectedCity.ShortDescOfDmg);
                }
                dbmanager.updateAffectedCity(AffectedCity.CityID1, AffectedCity.NameOfCity, AffectedCity.NoOfPopulation, AffectedCity.LvlOfDisaster, AffectedCity.SupportPersonnelNeeded, AffectedCity.ShortDescOfDmg);
            }
            
        }

        public static void viewCityProfile(DataGridView DGVViewCityProfile) 
        {
            dbmanager.viewCityProfile();
            try
            {
                DGVViewCityProfile.DataSource = null;
                DGVViewCityProfile.Refresh();
                if (AllFormClass.AffectedCity.Count > 0)
                {
                    DGVViewCityProfile.DataSource = AllFormClass.AffectedCity;
                    DGVViewCityProfile.Refresh();
                }
            }
            catch (NullReferenceException nre)
            {
                return;
            }
        }

        public static void DGVDelAffectedCity(DataGridView DGVDelAffectedCity)
        {
            dbmanager.viewCityProfile();
            try
            {
                DGVDelAffectedCity.DataSource = null;
                DGVDelAffectedCity.Refresh();
                if (AllFormClass.AffectedCity.Count > 0)
                {
                    DGVDelAffectedCity.DataSource = AllFormClass.AffectedCity;
                    DGVDelAffectedCity.Refresh();
                }
            }
            catch (NullReferenceException nre)
            {
                return;
            }
        }

        public static void view (TextBox tbxDeleteAffectedCitySearch)
        {
            dbmanager.viewCityProfile();
        }
        public static void viewUpdate(TextBox tbxUpdateAffectedCitySearch)
        {
            dbmanager.viewCityProfile();
        }
        public static void checkExist(TextBox tbxnameOfCity)
        {
            dbmanager.viewCityProfile();
        }

        public static void deleteSingleAffectedCity(ListBox searchDeleteAffectedCityResult, TextBox tbxDeleteAffectedCitySearch, Panel pnlDeleteAffectedCityDetails)
        {
            string selectedtitle = searchDeleteAffectedCityResult.SelectedItem.ToString();
            for (int i = 0; i < AllFormClass.AffectedCity.Count; i++)
            {
                AffectedCity a = (AffectedCity)AllFormClass.AffectedCity[i];
                if (a.NameOfCity == selectedtitle)
                {
                    dbmanager.deleteSingleCity(a.NameOfCity);
                    //AllFormClass.AffectedCity.RemoveAt(i);

                    MessageBox.Show("City removed successfully!");
                    tbxDeleteAffectedCitySearch.Clear();
                    pnlDeleteAffectedCityDetails.Hide();

                }
                
            }
        }

        public static void deleteMultiAffectedCity(ArrayList al, DataGridView DGVDelAffectedCity)
        {
            ArrayList integer = new ArrayList();
            foreach (DataGridViewRow row in DGVDelAffectedCity.SelectedRows)
            {
                integer.Add(row.Cells[0].Value);
            }

            for (int i = AllFormClass.AffectedCity.Count - 1; i >= 0; i--)
            {
                AffectedCity ac = (AffectedCity)AllFormClass.AffectedCity[i];
                
                for (int j = 0; j < integer.Count; j++)
                {
                    if (ac.CityID1 == Convert.ToInt32(integer[j]))
                    {
                        //AllFormClass.AffectedCity.RemoveAt(i);
                        dbmanager.deleteSingleCity(ac.NameOfCity);
                    }
                }
            }
            dbmanager.viewCityProfile();
            try
            {
                DGVDelAffectedCity.DataSource = null;
                DGVDelAffectedCity.Refresh();
                if (AllFormClass.AffectedCity.Count > 0)
                {
                    DGVDelAffectedCity.DataSource = AllFormClass.AffectedCity;
                    DGVDelAffectedCity.Refresh();
                }
            }
            catch (NullReferenceException nre)
            {
                return;
            }
            
           
        }
    }
}
